import { TaskState } from './TaskState';

export class Task {
    protected state: TaskState = TaskState.pending;

    getState(): TaskState {
        return this.state;
    }

    // 由节点外部调用
    reset() {
        this.state = TaskState.pending;
    }
    running(): void {
        if (this.state == TaskState.pending) {
            this.state = TaskState.running;
        } else {
            console.warn('错误流程 running', TaskState[this.state]);
        }
    }

    // 由节点内部调用
    done() {
        if (
            this.state == TaskState.running ||
            this.state == TaskState.pending
        ) {
            this.state = TaskState.done;
        } else {
            console.warn('错误流程 done', TaskState[this.state]);
        }
    }

    error() {
        if (
            this.state == TaskState.running ||
            this.state == TaskState.pending
        ) {
            this.state = TaskState.error;
        } else {
            console.warn('错误流程 error', TaskState[this.state]);
        }
    }
}
